<?php

namespace app\modules\admin\controllers;

use app\modules\admin\components\AccessControl;
use app\modules\admin\components\MenuHelper;
use app\modules\admin\models\form\Login;
use app\modules\admin\models\Sites;
use Yii;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use yii\filters\VerbFilter;
use yii\helpers\Url;

class SiteController extends \yii\web\Controller
{

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionLogin()
    {
        //var_dump(Yii::$app->user->isGuest);die;
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['route/index']);
        }
        $model = new Login();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['route/index']);
        }

        $model->password = '';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->redirect(['site/login']);;
    }


    public function actionTest()
    {
        $callback = function($menu){
            $data = json_decode($menu['data'], true);
            $items = $menu['children'];
            $return = [
                'label' => $menu['name'],
                'url' => [$menu['route']],
            ];
            //处理我们的配置
            if ($data) {
                //visible
                isset($data['visible']) && $return['visible'] = $data['visible'];
                //icon
                isset($data['icon']) && $data['icon'] && $return['icon'] = $data['icon'];
                //other attribute e.g. class...
                $return['options'] = $data;
            }
            //没配置图标的显示默认图标
            (!isset($return['icon']) || !$return['icon']) && $return['icon'] = 'fa fa-circle-o';
            $items && $return['items'] = $items;
            return $return;
        };
        //var_dump(Yii::$app->user->id);die;
       print_r(MenuHelper::getAssignedMenu(Yii::$app->user->id));
    }

    public function actionIndex()
    {
        $datas = (new Query())->select('option_key,option_value')->from('sites')->all();
        $data = $this->dataToView($datas);
        return $this->render('index', [
            'data' => $data
        ]);
    }

    public function actionUpdate()
    {
        if ($data = Yii::$app->request->post()) {
            $this->processingData($data['option']);
            return $this->redirect(Url::to(['site/index']));
        }
    }

    protected function dataToView($datas)
    {
        $data = [];
        foreach ($datas as $v) {
            $data[$v['option_key']] = $v['option_value'];
        }
        return $data;
    }

    protected function processingData($datas)
    {
        foreach ($datas as $k => $data) {
            Yii::$app->db->createCommand()->update('sites', [
                'option_value' => $data,
            ], 'option_key=:option_key', [':option_key' => $k])->execute();
        }
    }
}
